<?php
//------- logistic wxapp -------
namespace app\logi\service;

use app\logi\model\LogiPostModel;

class PostService {

    public function adminPostList($filter, $more = false) {
        $where = [
            'a.create_time' => ['>=', 0],
            'a.delete_time' => 0
        ];

        $join = [
            ['__USER__ u', 'a.user_id = u.id'],
            ['__LOGI_STATUS__ c', 'a.post_status = c.id','left'],
            ['__LOGI_MAIL_STATUS__ m', 'a.mail_status = m.id','left'],
        ];

        $field = 'a.*,u.user_login,u.user_nickname,u.user_email,c.name as s_name,m.name as m_name';

        $category = empty($filter['category']) ? 0 : intval($filter['category']);
        if (!empty($category)) {
            $where['b.category_id'] = ['eq', $category];
            array_push($join, [
                '__LOGI_CATEGORY_POST__ b', 'a.id = b.post_id'
            ]);
            $field = 'a.*,b.id AS post_category_id,b.list_order,b.category_id,u.user_login,u.user_nickname,u.user_email,c.name as s_name,m.name as m_name';
        }

        $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
        $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['a.published_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['a.published_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['a.published_time'] = ['<= time', $endTime];
            }
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.post_title'] = ['like', "%$keyword%"];
        }

        $status = empty($filter['status']) ? '' : $filter['status'];
        if (!empty($status)) {
            $where['a.post_status'] = ['eq',$status];
        }
        $mail_status = empty($filter['mail_status']) ? '' : $filter['mail_status'];
        if (!empty($mail_status)) {
            $where['a.mail_status'] = ['eq',$mail_status];
        }

        $logiPostModel = new LogiPostModel();
        $res = $logiPostModel->alias('a')->field($field)
            ->join($join)
            ->where($where)
            ->order('id', 'DESC')
            ->paginate(10);

        return $res;

    }

    public function publishedLogi($postId, $categoryId = 0) {
        $logiPostModel = new LogiPostModel();

        if (empty($categoryId)) {

            $where = [
                'post.delete_time'    => 0,
                'post.id'             => $postId
            ];

            $res = $logiPostModel->alias('post')->field('post.*')
                ->where($where)
                ->find();
        } else {
            $where = [
                'post.delete_time'     => 0,
                'relation.category_id' => $categoryId,
                'relation.post_id'     => $postId
            ];

            $join    = [
                ['__LOGI_CATEGORY_POST__ relation', 'post.id = relation.post_id']
            ];
            $res = $logiPostModel->alias('post')->field('post.*')
                ->join($join)
                ->where($where)
                ->find();
        }


        return $res;
    }

}